<?php


namespace app\app\controller;


use app\model\EquipmentModel;
use think\Controller;
use app\model\DeviceInfoModel;
class Index extends Controller
{
    public function index(){
        $this->redirect('Core/index');
    }

    /**
     * @return \think\response\View
     * 设备列表页面
     */
  public function device(){
      $imei =session('dev_name');
      $where=['equ_user'=>session('appuser.id')];
//      dump(session('appuser.id'));die();
      $data=EquipmentModel::where($where)->select()->toArray();
            $data_info =DeviceInfoModel::column('*','imei');
//            dump($data_info);die();

//      $data =DeviceInfoModel::alias('d')->field('d.*')
//          ->join('gps_equipment e','e.name = d.imei')
//          ->field('e.nickname,e.expire_time,e.head')
//          ->where('d.imei','in',$imei)
//          -> where('e.equ_user',session('appuser.id'))
//          ->select()->toArray();
      if($data){
          foreach ($data as $k=>$v){
                if(isset($data_info[$v['name']])){//已激活设备
                    $data[$k]['data_info']=$data_info[$v['name']];
                    $battery=$data_info[$v['name']]['battery'];
                    if($battery >95 && $battery <= 100){
                        $data[$k]['data_info']['img']='100';
                    }elseif ($battery >75 && $battery <= 95){
                        $data[$k]['data_info']['img']='80';
                    }elseif ($battery >55 && $battery <= 75){
                        $data[$k]['data_info']['img']='60';
                    }elseif ($battery >45 && $battery <= 55){
                        $data[$k]['data_info']['img']='50';
                    }elseif ($battery >10 && $battery <= 45){
                        $data[$k]['data_info']['img']='30';
                    }elseif ($battery >=0 && $battery <= 10){
                        $data[$k]['data_info']['img']='0';
                    }
                }

          }
      }
//      dump($data);die();
      $this->assign('data',$data);
      return view('index');
  }

    /**
     * @return array
     * 录音功能;
     */
  public function luyin(){
      $imei=input('imei');
      $expire_time=input('expire_time');
      //判断设备是否过期
      if($expire_time<=time()){
          $res['code']=203;
          $res['result']=['msg'=>'设备已过期'];
          return $res;
      }
      $arr = array('token'=>'4d9262b1a5da42779110baa9df648743');
      $url="http://api.huixun.me/open/record/$imei?token=4d9262b1a5da42779110baa9df648743";
      $data=post_json_data($url,json_encode($arr));
      $data['result']=json_decode($data['result'],true);
      return $data;
  }
    /*
      * 录音通话页面
      */
    public function luyin_view()
    {
        $getData=input();
        $data['imei']=$getData['imei'];
        $url="http://api.huixun.me/open/list/voice?imei={$getData['imei']}&token=4d9262b1a5da42779110baa9df648743&orderBy=desc";
        $list = $this->curl($url);

        foreach ($list['data'] as $k=>$v){
            $list['data'][$k]['createTime']=$v['createTime']/1000;

            $list['data'][$k]['attrJson']="http://file.huixun.me/".json_decode($v['attrJson'],true)['url'];
        }
        $data['data']=$list['data'];
        $this->assign('data',$data);
        return view('luyin_view');
    }

    /**
     * 拨打电话
     */
    public function dianhua(){
        $imei=input('imei');
        $type=input('type');
        $expire_time =input('expire_time');
        //判断设备是否过期
        if($expire_time<=time()){
            $res['code']=203;
            $res['result']=['msg'=>'设备已过期'];
            return $res;
        }
        $phone = session('appuser.phone');
        $arr = array('token'=>'4d9262b1a5da42779110baa9df648743','phone'=>$phone,'type'=>$type);
        $url="http://api.huixun.me/open/telephone/$imei?token=4d9262b1a5da42779110baa9df648743&phone={$phone}&type=$type";
        $data=post_json_data($url,json_encode($arr));
        $data['result']=json_decode($data['result'],true);
        return $data;
    }


    /**
     * 模拟post进行url请求
     * @param string $url
     * @param array $post_data
     */
    function request_post($url = '', $post_data = array()) {
        if (empty($url) || empty($post_data)) {
            return false;
        }
        $o = "";
        foreach ( $post_data as $k => $v )
        {
            $o.= "$k=" . urlencode( $v ). "&" ;
        }
        $post_data = substr($o,0,-1);

        $postUrl = $url;
        $curlPost = $post_data;
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        $data = curl_exec($ch);//运行curl
        curl_close($ch);

        return $data;
    }
    /**
     * @param $url   接口连接的地址
     * @return mixed
     *  转接接口的方法
     */
    public function curl($url){
        $ch = curl_init();
        //设置选项，包括URL
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);

        //执行并获取HTML文档内容
        $output = curl_exec($ch);

        //释放curl句柄
        curl_close($ch);

        $data=json_decode($output,true);//$data就是最终的数据 而且已经转换成了数组
        return $data;
    }

    //下载文件
    public function testDown()
    {
        $file_url= input('urldata');//下载地址
        $imei=input('imei');//设备名

        $scurl= ROOT_PATH . 'public' . DS . 'upload' . DS.'luyin'.DS.$imei;
        $dir = iconv("UTF-8", "GBK", $scurl);
        if (!file_exists($dir)){
            mkdir ($dir,0777,true);
        }
        $scurl=$scurl.DS;
        $fileName = downFile($file_url,$scurl);
        return retJson('/upload/luyin/'.$imei.'/'.$fileName,200,'成功');
    }

}